using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace FileFolderNavigator
{
    public class ProjectElement
    {
        private XmlElement _elem;

        public string Name
        {
            get { return _elem.GetAttribute("name"); }
            set { _elem.SetAttribute("name", value); }
        }

        public LocationElement Source
        {
            get
            {
                XmlElement elem = (XmlElement)_elem.SelectSingleNode("loc[@type='src']");
                return new LocationElement(elem);
            }
        }

        public LocationElement Destination
        {
            get
            {
                XmlElement elem = (XmlElement)_elem.SelectSingleNode("loc[@type='dst']");
                return new LocationElement(elem);
            }
        }

        public LocationElement XmlDoc
        {
            get
            {
                XmlElement elem = (XmlElement)_elem.SelectSingleNode("loc[@type='xml']");
                return new LocationElement(elem);
            }
        }

        public ActionElement Action
        {
            get
            {
                XmlElement elem = (XmlElement)_elem.SelectSingleNode("./action");
                return new ActionElement(elem);
            }
        }

        public void Delete()
        {
            _elem.ParentNode.RemoveChild(_elem);
        }

        public ProjectElement(XmlElement elem)
        {
            _elem = elem;
        }

        public override string ToString()
        {
            return Name;
        }

        public static ProjectElement CreateNewProject(XmlDocument doc)
        {
            XmlElement proj = doc.CreateElement("project");
            doc.FirstChild.AppendChild(proj);
            ProjectElement proje = new ProjectElement(proj);
            LocationElement src = LocationElement.CreateNewLocation(doc, "", "", "src");
            LocationElement dst = LocationElement.CreateNewLocation(doc, "", "", "dst");
            LocationElement xml = LocationElement.CreateNewLocation(doc, "", "", "xml");
            ActionElement act = ActionElement.CreateNewAction(doc);

            proj.AppendChild(src._elem);
            proj.AppendChild(dst._elem);
            proj.AppendChild(xml._elem);
            proj.AppendChild(act._elem);

            return proje;
        }
    }

    public class LocationElement
    {
        public XmlElement _elem;

        public string Name
        {
            get { return _elem.GetAttribute("name"); }
            set { _elem.SetAttribute("name", value); }
        }

        public string Path
        {
            get { return _elem.GetAttribute("path"); }
            set { _elem.SetAttribute("path", value); }
        }

        public string Type
        {
            get { return _elem.GetAttribute("type"); }
            set { _elem.SetAttribute("type", value); }
        }

        public LocationElement(XmlElement elem)
        {
            _elem = elem;
        }

        public static LocationElement CreateNewLocation(XmlDocument doc)
        {
            XmlElement elem = doc.CreateElement("loc");
            return new LocationElement(elem);
        }

        public static LocationElement CreateNewLocation(XmlDocument doc, string path, string name, string type)
        {
            XmlElement elem = doc.CreateElement("loc");
            LocationElement loc = new LocationElement(elem);
            loc.Name = name;
            loc.Path = path;
            loc.Type = type;
            return loc;
        }
    }


    public class ActionElement
    {
        public XmlElement _elem;

        public string Days
        {
            get { return _elem.GetAttribute("days"); }
            set { _elem.SetAttribute("days", value); }
        }

        public string Time
        {
            get { return _elem.GetAttribute("time"); }
            set { _elem.SetAttribute("time", value); }
        }

        public string Last
        {
            get { return _elem.GetAttribute("last"); }
            set { _elem.SetAttribute("last", value); }
        }

        public string Type
        {
            get { return _elem.GetAttribute("type"); }
            set { _elem.SetAttribute("type", value); }
        }

        public ActionElement(XmlElement elem)
        {
            _elem = elem;
        }

        public static ActionElement CreateNewAction(XmlDocument doc)
        {
            return new ActionElement(doc.CreateElement("action"));
        }

        public static ActionElement CreateNewAction(XmlDocument doc,string days,string time,string last,string type)
        {
            ActionElement act = ActionElement.CreateNewAction(doc);
            act.Days = days;
            act.Time = time;
            act.Type = type;
            act.Last = last;
            return act;
        }
    }
}
